package com.sanuo.pico.act;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.menu.app.activity.BaseBoardActivity;
import com.menu.app.activity.BaseCustomInfoActivity;
import com.menu.app.adapter.BaseCusRecordAdapter;
import com.menu.app.bean.DayRecord;
import com.menu.app.db.bean.CusUsage;
import com.menu.app.db.bean.Custom;
import com.menu.app.db.bean.Doc;
import com.menu.app.fragment.AlertDlg;
import com.menu.app.fragment.BaseDialogFragment;
import com.menu.app.util.Utils;
import com.sanuo.pico.App;
import com.sanuo.pico.R;
import com.sanuo.pico.act.work.WarnAct;
import com.sanuo.pico.adapter.CusRecordAdapter;
import com.sanuo.pico.bean.WorkParam;
import com.sanuo.pico.event.Event;
import com.sanuo.pico.frag.ParamDlg;
import com.sanuo.pico.utils.Command;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class CustomInfoActivity extends BaseCustomInfoActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Command.getHandleType();
    }

    @Override
    protected BaseCusRecordAdapter getAdapter() {
        return new CusRecordAdapter(this, dao, custom, binding.list, isAdmin);
    }

    @Override
    protected String createPdf(Doc doc, Custom custom, List<DayRecord> list) {
        return "";
    }

    @Override
    protected List<DayRecord> combine(List<List<CusUsage>> l, Map<String, Integer> map) {
        List<DayRecord> list = new LinkedList<>();
        for (String key : map.keySet()) {
            DayRecord r = new DayRecord();
            r.setDay(key);
            r.record = l.get(map.get(key));
            list.add(r);
        }
        Iterator<DayRecord> it = list.iterator();
        while (it.hasNext()) {
            DayRecord r = it.next();
            if (r.img.isEmpty() && (r.record == null || r.record.isEmpty())) {
                it.remove();
            }
        }
        Collections.sort(list, (o1, o2) -> {
            long t1 = date2Ms(o1.getDay(0));
            long t2 = date2Ms(o2.getDay(0));
            if (t1 < t2) {
                return 1;
            } else if (t1 == t2) {
                return 0;
            }
            return -1;
        });
        return list;
    }

    @Override
    public void showReport(DayRecord record) {

    }

    @Override
    public void showParam(List<CusUsage> item) {
        ParamDlg dlg = new ParamDlg();
        dlg.setParam(item, custom, isAdmin);
        dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
            @Override
            public void onDismiss(boolean isOk, Object value) {
                if (!dlg.getDelete().isEmpty()) {
                    for (Long id : dlg.getDelete()) {
                        dao.delete(id);
                    }
                    getData();
                }
                if (isOk && value instanceof CusUsage) {
                    if (Event.warn.hasWarn()) {
                        Intent i = new Intent(CustomInfoActivity.this, WarnAct.class);
                        startActivity(i);
                        return;
                    }
                    toWork((CusUsage) value);
                }
            }
        });
        dlg.show(getSupportFragmentManager(), "param");
    }

    @Override
    protected boolean checkHandle(Runnable runnable) {
        return true;
    }

    @Override
    public void startWork(CusUsage data) {
        Gson gson = Utils.getGson();
        WorkParam param = gson.fromJson(gson.toJson(data.info), WorkParam.class);
        toWork(param);
    }

    @Override
    protected void onResume() {
        super.onResume();
        App.getApp().setIgnore(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseBoardActivity.custom = null;
        App.getApp().setIgnore(true);
        App.getApp().setWorkParam(null);
    }

    @Override
    protected void onPause() {
        super.onPause();
        hideLoading();
        h.removeCallbacksAndMessages(null);
    }

    private void toWork(WorkParam param) {
        if (Event.handle.getValue() != param.handle) {
            new AlertDlg().setMessage(getString(R.string.cannot_reuse)).showCancelBtn(false).show(getSupportFragmentManager(), "");
            return;
        }
        BaseBoardActivity.custom = custom;
        App.getApp().setWorkParam(param);
        App.getApp().setIgnore(false);
        Command.sendTouchEvent(0, 0x11);
    }
}
